﻿using Inovout;
using Inovout.Web.Mvc;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc.Properties;
using System.Web.Routing;

namespace System.Web.Mvc.Html
{
    public static class WidgetActionExpressions
    {
        public static IHtmlString Submit(this HtmlHelper html, string value, object htmlAttributes = null)
        {
            return InputButtonInternal(html, true, value, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static IHtmlString SecuritySubmit(this HtmlHelper html, string value, object htmlAttributes = null)
        {
            return InputButtonInternal(html, true, value, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }


        public static IHtmlString Button(this HtmlHelper html, string value, object htmlAttributes = null)
        {
            return InputButtonInternal(html, false, value, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        //public static IHtmlString SecurityActionLink(this HtmlHelper html, string linkText, string actionName, string controllerName)
        //{
        //    if (PermissionHelp.CheckPermisson(HttpContext.Current.Request.Url.Host + "/" + controllerName + "/" + actionName, "get"))
        //    {
        //        return html.ActionLink(linkText, actionName, controllerName);
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}

        public static MvcHtmlString SecurityActionLink(this HtmlHelper htmlHelper, string linkText, string actionName)
        {
            return SecurityActionLink(htmlHelper, linkText, actionName, null /* controllerName */, new RouteValueDictionary(), new RouteValueDictionary());
        }

        public static MvcHtmlString SecurityActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues)
        {
            return SecurityActionLink(htmlHelper, linkText, actionName, null /* controllerName */, TypeHelper.ObjectToDictionary(routeValues), new RouteValueDictionary());
        }

        public static MvcHtmlString SecurityActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
        {
            return SecurityActionLink(htmlHelper, linkText, actionName, null /* controllerName */, TypeHelper.ObjectToDictionary(routeValues), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static MvcHtmlString SecurityActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues)
        {
            return SecurityActionLink(htmlHelper, linkText, actionName, null /* controllerName */, routeValues, new RouteValueDictionary());
        }

        public static MvcHtmlString SecurityActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            return SecurityActionLink(htmlHelper, linkText, actionName, null /* controllerName */, routeValues, htmlAttributes);
        }

        public static MvcHtmlString SecurityActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
        {
            return SecurityActionLink(htmlHelper, linkText, actionName, controllerName, new RouteValueDictionary(), new RouteValueDictionary());
        }

        public static MvcHtmlString SecurityActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
        {
            //string url = UrlHelper.GenerateUrl(routeName, actionName, controllerName, protocol, hostName, fragment, routeValues, routeCollection, requestContext, includeImplicitMvcValues);

            var uri = HttpContext.Current.Request.Url;
            string url = string.Format("{0}://{1}{2}", uri.Scheme, uri.Authority, "/" + controllerName + "/" + actionName);
            if (PermissionHelp.CheckPermisson(url, "get"))
            {
                return htmlHelper.ActionLink(linkText, actionName, controllerName, TypeHelper.ObjectToDictionary(routeValues), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
            }
            else
            {
                return null;
            }
        }

        public static MvcHtmlString SecurityActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, object routeValues, object htmlAttributes)
        {
            return SecurityActionLink(htmlHelper, linkText, actionName, controllerName, protocol, hostName, fragment, TypeHelper.ObjectToDictionary(routeValues), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static MvcHtmlString SecurityActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            if (PermissionHelp.CheckPermisson(new UrlHelper().Action(actionName, controllerName, routeValues), "get"))
            {
                return htmlHelper.ActionLink(linkText, actionName, controllerName, protocol, hostName, fragment, routeValues, htmlAttributes);
            }
            else
            {
                return null;
            }
        }



        internal static IHtmlString InputButtonInternal(this HtmlHelper html, bool isSubitButton, string value, IDictionary<string, object> htmlAttributes)
        {
            TagBuilder tagBuilder = new TagBuilder("button");
            tagBuilder.MergeAttribute("type", isSubitButton ? "submit" : "button");
            tagBuilder.MergeAttributes(htmlAttributes, true);
            //tagBuilder.MergeAttribute("value", value);
            tagBuilder.InnerHtml = value;
            return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
        }



    }
    public enum ButtonType
    {
        InputButton,
        InputSubmit,

    }
}
